package com.gd.iot.energy.util;

import com.gd.iot.energy.code.ResponseCode;
import com.gd.iot.energy.consts.SystemConsts;
import com.gd.iot.energy.exception.MyException;
import com.gd.iot.energy.pojo.common.vo.ColumnVO;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Element;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * @author kris
 */
@Slf4j
public class CustomColumnUtil {

    /**
     * @param fileName .column文件全路径
     * @return
     * @Title: getTrColumn
     * @Description: 读取.colum文件，返回表头字段、单位、样式集合
     * @return: List<TRColumn>
     */
    public static List<ColumnVO> getColumn(String fileName) {
        ClassPathResource classPathResource = new ClassPathResource(SystemConsts.CONFIG_DIR + fileName);
        InputStream inputStream = null;
        try {
            inputStream = classPathResource.getInputStream();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new MyException(ResponseCode.SYSTEM_ERROR);
        }

        Element columnXML = CustomDomResolveUtil.parseXml(inputStream);

        List<ColumnVO> columnList = new ArrayList<>();
        if (columnXML == null) {
            return new ArrayList<>(0);
        }
        List<Element> tableList = columnXML.elements("table");
        if (tableList.isEmpty()) {
            return new ArrayList<>(0);
        }
        for (Element table : tableList) {
            List<Element> columnEleList = table.elements("col");
            if (columnEleList.isEmpty()) {
                continue;
            }

            for (Element elem : columnEleList) {
                ColumnVO col = new ColumnVO();

                setCol(elem, col);

                columnList.add(col);
            }


        }

        return columnList;
    }

    private static void setCol(Element elem, ColumnVO col) {
        String languageKey = elem.attributeValue("languageKey");
        String pro = elem.attributeValue("pro");
        String ignore = elem.attributeValue("ignore");
        String show = elem.attributeValue("show");
        String width = elem.attributeValue("width");
        String index = elem.attributeValue("index");
        String unit = elem.attributeValue("unit");
        String valueType = elem.attributeValue("valueType");
        String sort = elem.attributeValue("sort");
        String riseOrDrop = elem.attributeValue("riseOrDrop");
        String dateType = elem.attributeValue("dateType");
        String booNormal = elem.attributeValue("booNormal");
        if (!StringUtils.isEmpty(languageKey)) {
            col.setName(languageKey);
        }
        if (!StringUtils.isEmpty(pro)) {
            col.setKey(pro);
        }
        if (!StringUtils.isEmpty(ignore)) {
            col.setIgnore(Boolean.valueOf(ignore));
        }else{
            col.setIgnore(false);
        }
        if (!StringUtils.isEmpty(show)) {
            col.setShow(Boolean.valueOf(show));
        }else{
            col.setShow(true);
        }

        if (!StringUtils.isEmpty(width)) {
            col.setWidth(width);
        }

        if (!StringUtils.isEmpty(index)) {
            col.setIndex(index);
        }
        if (!StringUtils.isEmpty(unit)) {
            col.setUnit(unit);
        }
        if (!StringUtils.isEmpty(valueType)) {
            col.setValueType(valueType);
        }
        if (!StringUtils.isEmpty(sort)) {
            col.setSort(sort);
        }
        if (!StringUtils.isEmpty(riseOrDrop)) {
            col.setRiseOrDrop(Integer.valueOf(riseOrDrop));
        }
        if (!StringUtils.isEmpty(dateType)) {
            col.setDateType(dateType);
        }
        if (!StringUtils.isEmpty(booNormal)) {
            col.setBooNormal(booNormal);
        }
    }
}
